package com.bianmin.Controller;

import com.bianmin.Data.Result;
import com.bianmin.Mapper.QuantityRecordsMapper;
import com.bianmin.Mapper.SignInRecordsMapper;
import com.bianmin.Mapper.UserMapper;
import com.bianmin.Model.QuantityRecords;
import com.bianmin.Model.SignInRecords;
import com.bianmin.Model.User;
import com.bianmin.coomen.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;

/*
* 签到相关
* */
@RestController
@RequestMapping("api/signInRecoeds")
public class SignInRecordsController {

    @Autowired
    private SignInRecordsMapper mapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private QuantityRecordsMapper quantityRecordsMapper;

    //签到接口
    @PostMapping("signIn")
    public Result<Boolean> signIn(@RequestBody SignInRecords sign) throws ParseException {

        String currentTime = Util.currentTimeString();
        sign.setSignInDate(currentTime);
        mapper.insert(sign);

        //更新用户积分
        User user = userMapper.queryUserWithUserId(sign.getUserId());
        Integer quantity = user.getQuantity() + 30;
        user.setQuantity(quantity);
        userMapper.updateUserQuantity(quantity,user.getId());

        //添加变动记录
        QuantityRecords records = new QuantityRecords();
        records.setUserId(user.getId());
        records.setCreateTime(Util.currentTimeString());
        records.setRecord("签到增加 30 积分");
        quantityRecordsMapper.insert(records);
        return Result.success(true);
    }

    //查看今日签到状态
    @GetMapping("checkTodaySignStatus")
    public Result<Boolean> checkTodaySignStatus(Integer userId) throws ParseException {
        String lastTime = mapper.selectUserLastSignInTime(userId);
        String time = Util.currentTimeString();
        if (lastTime == null) {
            return Result.success(false);
        }
        //true已签到。false未签到
        return Result.success(Util.inSameDay(time,lastTime));
    }




}
